package com.pengpeng.android.dao;

import java.util.ArrayList;
import java.util.Date;

import android.database.Cursor;

import com.pengpeng.android.entity.MyselfEntity;

public class MyselfDao extends SuperDao{

	public MyselfDao()
	{

    	openDatabase();
    	if(!existTable("myself"))
    	{
    		createTable();
    	
    	}
	}//FriendDao
    
	private void createTable()
	{
		String sqlstr="create table myself(";
		sqlstr+="phonenum  varchar(20) not null,";
		sqlstr+="name  varchar(20)  null,";
		sqlstr+="ifonline varchar(2) null,";
		sqlstr+="location  varchar(40)  null,";
		sqlstr+="locattime datetime  null,";
		sqlstr+="locatcontent varchar(80) null,";
		sqlstr+="ifsendmyloc varchar(2) null,";
		sqlstr+="ifmanualloc varchar(2) null,";
		sqlstr+="manualloction varchar(40) null,";
		sqlstr+="constraint PK_FRIENDS primary key clustered (phonenum)";
		sqlstr+=");";
		sqlitedb.execSQL(sqlstr);
	}//createTable
	
	
    private void insert(MyselfEntity myselfentity)
    {
    	String sqlstr="insert into myself(";
    	sqlstr+="phonenum,name,ifonline,location,locattime,locatcontent,ifsendmyloc,ifmanualloc,manualloction) ";
    	sqlstr+="values('"+myselfentity.getPhonenum()+"','";
    	sqlstr+=myselfentity.getName()+"','";
    	sqlstr+=myselfentity.getIfonline()+"','";
    	sqlstr+=myselfentity.getLocation()+"','";
    	sqlstr+=myselfentity.getLocattime().toString()+"','";
    	sqlstr+=myselfentity.getLocatcontent()+"','";
    	sqlstr+=myselfentity.getIfsendmyloc()+"')";
    	sqlstr+=myselfentity.getIfmanualloc()+"')";
    	sqlstr+=myselfentity.getManuallocation()+"')";
		sqlitedb.execSQL(sqlstr);
    }//insert
    
    private void delete(MyselfEntity myselfentity)
    {
    	String sqlstr="delete from myself";
    	sqlstr+="where phonenum='"+myselfentity.getPhonenum()+"'";
		sqlitedb.execSQL(sqlstr);
    }//delete
    
    public void update(MyselfEntity myselfentity)
    {
    	String sqlstr="update myself set ";
    	sqlstr+="phonenum='"+myselfentity.getPhonenum()+"',";
    	sqlstr+="ifonline='"+myselfentity.getIfonline()+"',";
    	sqlstr+="location='"+myselfentity.getLocation()+"',";
    	sqlstr+="locattime='"+myselfentity.getLocattime().toString()+"',";
    	sqlstr+="locatcontent='"+myselfentity.getLocatcontent()+"',";
    	sqlstr+="ifsendmylocc='"+myselfentity.getIfsendmyloc()+"' ";
    	sqlstr+="ifmanualloc='"+myselfentity.getIfmanualloc()+"' ";
    	sqlstr+="manualloction='"+myselfentity.getManuallocation()+"' ";
    	sqlstr+="where phonenum='"+myselfentity.getPhonenum()+"'";
		sqlitedb.execSQL(sqlstr);	
    }//update
    
    public MyselfEntity query()
    {
    	String sqlstr="";
		Cursor cursor=null;
		int result=0;
		MyselfEntity myselfentity=null;;
		sqlstr="select phonenum,name,ifonline,location,locattime,";
		sqlstr+="locatcontent,ifsendmyloc,ifmanualloc,manualloction ";
		sqlstr+="from myself";
		cursor=sqlitedb.rawQuery(sqlstr, null);
		cursor.moveToFirst();
		if(!cursor.isAfterLast())
		{    
			String phonenum=cursor.getString(0);
			String name=cursor.getString(1);
			String ifinline=cursor.getString(2);
			String location=cursor.getString(3);
			Date locattime=new Date(cursor.getString(4));
			String locatcontent=cursor.getString(5);
			String ifsendmyloc=cursor.getString(6);
			String ifmanualloc=cursor.getString(7);
			String manuallocation=cursor.getString(8);
			
			myselfentity=new MyselfEntity();
			myselfentity.setIfonline(ifinline);
			myselfentity.setIfsendmyloc(ifsendmyloc);
			myselfentity.setIfmanualloc(ifmanualloc);
			myselfentity.setManuallocation(manuallocation);
			myselfentity.setLocatcontent(locatcontent);
			myselfentity.setLocation(location);
			myselfentity.setLocattime(locattime);
			myselfentity.setName(name);
			myselfentity.setPhonenum(phonenum);
			
			
						
		}
		cursor.close();
    	 
        return myselfentity;
    }//query
    

    
    public void close()
    {
    	closeDatabase();
    }//close


}
